﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using KerriganInterface.IServiceClient;
using KerriganDataContract;
using KerriganService.DAC;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.IO;
using KerriganInterface;
using KerriganDataContract.Person;
using KerriganDataContract.Customer;
using KerriganDataContract.Intructor;
using KerriganDataContract.Vehicule;
using KerriganDataContract.Session;
using KerriganDataContract.Formation;

namespace KerriganService.ServiceClient
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class DirectionService : IDirectionService, IPolicyRetriever
    {
        #region Cross Domain

        public Stream GetSilverlightPolicy()
        {
            return StringToStream(CommonConstant.clientaccesspolicy);
        }
        public Stream GetFlashPolicy()
        {
            return StringToStream(CommonConstant.crossdomain);
        }

        Stream StringToStream(string result)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }

        #endregion

        #region Person

        public PersonDC GetPerson(int TheId)
        {
            PersonDC dc = new PersonDC();

            try
            {
                return dc = PersonDAC.GetPerson(TheId);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region Customer

        public CustomerDC GetCustomer(int id)
        {
            throw new NotImplementedException();
        }

        public void AddCustomer(CustomerDC dc)
        {
            try
            {
                CustomerDAC.AddCustomer(dc);
            }
            catch (Exception ex)
            {
                throw new FaultException("Un message d'erreur exemple.");
            }
        }

        public void DeleteCustomer(CustomerDC dc)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Session

        public List<vwGetSession> GetListSessionsToDisplay()
        {
            try
            {
                return SessionCoursesDAC.GetListSessionsToDisplay();
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public SessionDC GetSession(int id)
        {
            try
            {
                return SessionCoursesDAC.GetSession(id);
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public void DeleteSession(int id)
        {
            try
            {
                SessionCoursesDAC.DeleteSession(id);
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }
        }

        public void SetSession(SessionDC bo)
        {
            try
            {
                SessionCoursesDAC.SetSession(bo);
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }
        }

        public List<FormationDC> GetListFormations()
        {
            try
            {
                return FormationDAC.GetListFormations();
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public List<LevelSessionDC> GetListLevelSessions()
        {
            try
            {
                return LevelSessionDAC.GetListLevelSessions();
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public List<FormationCategoryDC> GetFormationCategories()
        {
            try
            {
                return FormationCategoryDAC.GetFormationCategories() ;
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        #endregion

        #region Vehicle

        public List<vwGetVehicle> GetListVehiclesToDisplay()
        {
            try
            {
                return VehicleDAC.GetListVehiclesToDisplay();                
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public List<vwGetVehicleAvailability> GetListVehiclesAvailabilityToDisplay()
        {
            try
            {
                return VehicleDAC.GetListVehiclesAvailabilityToDisplay();
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public VehicleDC GetVehicle(int id)
        {
            try
            {
                return VehicleDAC.GetVehicle(id);
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public void DeleteVehicle(int id)
        {
            try
            {
                VehicleDAC.DeleteVehicle(id);
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }
        }

        public void SetVehicle(VehicleDC bo)
        {
            try
            {
                VehicleDAC.SetVehicle(bo);
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }
        }


        public List<VehicleDC> GetListVehicle()
        {
            try
            {
                return VehicleDAC.GetListVehicles();
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public List<TypeVehicleDC> GetListTypeVehicle()
        {
            try
            {
                return TypeVehicleDAC.GetListTypeVehicle();
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public List<GetVehicleAvailableByDate_Result> GetVehicleAvailableByDate(DateTime StartDate, DateTime EndDate)
        {
            try
            {
                return VehicleDAC.GetVehicleAvailableByDate(StartDate, EndDate) ;
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        #endregion

        #region Instructor

        public List<vwGetInstructor> GetListInstructorsToDisplay()
        {
            try
            {
                return InstructorDAC.GetListInstructorsToDisplay();
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public InstructorDC GetInstructor(int id)
        {
            try
            {
                return InstructorDAC.GetInstructor(id);
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public void DeleteInstructor(int id)
        {
            try
            {
                InstructorDAC.DeleteInstructor(id);
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }
        }

        public void SetInstructor(InstructorDC bo)
        {
            try
            {
                InstructorDAC.SetInstructor(bo);
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }
        }

        
        public List<InstructorDC> GetListInstructor()
        {
            try
            {
                return InstructorDAC.GetListInstructor();
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public List<InstructorAvailabilityDC> GetListInstructorAvailable()
        {
            try
            {
                return InstructorDAC.GetListInstructorAvailable();
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public void AddAvaibilityForInstructor(InstructorAvailabilityDC dc)
        {
            try
            {
                InstructorDAC.AddAvaibilityForInstructor(dc);
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }
        }

        public void DeleteInstructorAvaibility(int id)
        {
            try
            {
                InstructorDAC.DeleteInstructorAvaibility(id);
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }
        }

        public List<GetInstructorAvaibleBySession_Result> GetListInstructorBySession(SessionDC bo)
        {
            try
            {
                return InstructorDAC.GetListInstructorBySession(bo);
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }

            return null;
        }

        public List<vwGetInstructorAvailability> GetListInstructorsAvailabilityToDisplay()
        {
            List<vwGetInstructorAvailability> list = new List<vwGetInstructorAvailability>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    list = db.vwGetInstructorAvailabilities.ToList();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        #endregion

        #region Agency

        public List<KerriganDataContract.Agency.AgencyDC> GetListAgency()
        {
            try
            {
                return AgencyDAC.GetListAgency();
            }
            catch (Exception ex)
            {
                new FaultException(ex.Message);
            }
            
            return null;
        }
        #endregion 
    }
}
